@extends('Admin.base')
@section('css')
    <link rel="stylesheet" href="/Admin/layuiadmin/css/extend/formSelects-v4.css" media="all">
@endsection
@section('style')
    <style type="text/css">
        .textc {
            width: 80px;
            height: 40px;
        }
    </style>
@endsection
@section('content')
    <div class="layui-card layui-form">

        <div class="layui-card-header layuiadmin-card-header-auto">
            <fieldset class="layui-elem-field layui-field-title">
                <legend>会员等级设置</legend>
            </fieldset>
        </div>

        <div class="layui-card-body">
            <form id="levelset_submit">
                <table id="layTable" lay-filter="layTable"></table>
                <br>
            </form>
            <div class="layui-btn-container">
                <button type="reset" id="reset" class="layui-btn layui-btn-primary">重置</button>
                <button class="layui-btn" lay-submit lay-filter="laySubmit">立即提交</button>
            </div>


        </div>


    </div>

@endsection

@section('script')
    <script src="/js/ajax.js"></script>
    <script type="text/html" id="switchTpl">
        <input type="checkbox" name="data@{{d.id}}[status]" value="@{{d.status}}" lay-skin="switch" lay-text="开启|关闭"
               lay-filter="sexDemo" @{{ d.status== 1 ? 'checked' : '' }}>
    </script>

    <script type="text/html" id="levelnameTpl">
        @{{# if(d.id == 1){  }}
        粉丝
        @{{# }else{ }}
        <div> @{{d.level_name}}级会员</div>
        @{{# } }}
    </script>


    <script type="text/html" id="requiremeTpl">
        @{{# if(d.id == 1){  }}
        注册
        @{{# }else{ }}
        <div>充值达&nbsp;&nbsp;&nbsp;<input type="text" lay-verify="number|validateInt" name="data@{{d.id}}[money]"
                                         value="@{{d.money}}" lay-filter="moneyFilter" class="textc">&nbsp;&nbsp;&nbsp;元
        </div>
        @{{# } }}
    </script>
    <script type="text/html" id="deadlineTpl">
        @{{# if(d.id == 1){  }}
        永久
        @{{# }else{ }}
        <div><input type="text" name="data@{{d.id}}[deadline]" lay-verify="number|validateInt" value="@{{d.deadline}}"
                    class="textc">&nbsp;&nbsp;&nbsp;天
        </div>
        @{{# } }}
    </script>
    <script type="text/html" id="discountTpl">
        <div><input type="text" name="data@{{d.id}}[discount]" lay-verify="number|validateFloat" value="@{{d.discount}}"
                    class="textc">&nbsp;&nbsp;&nbsp;折
        </div>
    </script>
    <script type="text/html" id="idTpl">
        <div><input type="text" name="data@{{d.id}}[id]" value="@{{d.id}}" class="textc">&nbsp;&nbsp;&nbsp;折</div>
    </script>
    <script>
        layui.use(['form', 'table', 'formSelects', 'laydate'], function () {
            var $ = layui.$
                , form = layui.form
                , table = layui.table
                , formSelects = layui.formSelects
                , laydate = layui.laydate;


            table.render({
                elem: '#layTable'
                , url: "{{route('user.levelsettable')}}"
                , method: "get"
                , page: false //开启分页
                , limit: 10
                , cols: [[
                    , {field: 'id', title: 'ID', hide: true, templet: '#idTpl',}
                    , {field: 'level_name', title: '会员级别', width: 200, templet: '#levelnameTpl',}
                    , {field: 'money', title: '成为条件', templet: '#requiremeTpl',}
                    , {field: 'deadline', title: '会员期限', templet: '#deadlineTpl',}
                    , {field: 'discount', title: '折扣权限', templet: '#discountTpl',}
                    , {field: 'status', title: '启用状态', templet: '#switchTpl', width: 380}
                ]]

            });


            //自定义表单验证（金额）
            form.verify({
                validateInt: [
                    /^[+]{0,1}(\d+)$/  //正则表达式
                    , '只能为正整数'  //提示信息
                ],
                validateFloat: [
                    /^\d+\.?\d{0,2}$/  //正则表达式
                    , '只能为正数,并且小数点保留两位'  //提示信息
                ],
            })

            //监听
            form.on('submit(laySubmit)', function (data) {
                console.log(form);
                var field = data.field;
                $.ajax({
                    url: "{{route('user.levelset')}}"
                    , type: 'POST'
                    , dataType: 'json'
                    , data: field
                    , success: function (ret) {
                        if (ret.hasOwnProperty('code') && ret.code == 200) {
                            layer.msg(ret.message, {icon: 1, time: 1000}, function () {
                                location.reload();
                            });
                            return false;
                        }
                        layer.msg(ret.message, {icon: 2});
                    }
                    , complete: function () {
                        layer.closeAll('loading');
                    }
                });


            });


        });


      // 重置事件
        $("#reset").click(function () {
            window.location.reload();
        })



    </script>

@endsection